package com.huilian.iotbox.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.huilian.iotbox.admin.dao.StatsCommodityStockRecordDao;
import com.huilian.iotbox.admin.service.CommodityStockRecordService;
import com.huilian.iotbox.admin.service.StatsCommodityStockRecordService;
import com.huilian.iotbox.data.dto.StatsCommodityStockRecordDto;
import com.huilian.iotbox.data.dto.StatsPackageStoreDto;
import com.huilian.iotbox.data.enums.StatisticsTypeEnum;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.CommodityStockRecordVo;
import com.huilian.iotbox.data.vo.StatsCommodityStockRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 龙 路 飞
 * @CreateDate: 2023-08-31 15:34
 * @Description:
 */
@Service
@Slf4j
public class StatsCommodityStockRecordServiceImpl implements StatsCommodityStockRecordService {
    @Resource
    private StatsCommodityStockRecordDao statsCommodityStockRecordDao;
    @Autowired
    private CommodityStockRecordService commodityStockRecordService;
    @Resource
    private ErrorLogCommonService errorLogCommonService;

    /**
     * 每日统计昨天的商品库存记录
     */
    @Override
    public void statsDayCommodityStockRecordJob() {
        //获取昨天 1970-01-01 00：00：00
        String startTime = DateTimeUtils.getYesterdayStartTime();
        //获取昨天 1970-01-01 23：59：59
        String endTime = DateTimeUtils.getYesterdayEndTime();

        // 昨天，1970-01-01
        String yesterday = DateTimeUtils.getYesterday();
        try {
            List<CommodityStockRecordVo> list = commodityStockRecordService.findCommodityStockRecordByTime(startTime, endTime);

            for (CommodityStockRecordVo commodityStockRecordVo : list) {
                StatsCommodityStockRecordDto statsCommodityStockRecordDto = new StatsCommodityStockRecordDto();
                statsCommodityStockRecordDto.setDateStr(yesterday);
                statsCommodityStockRecordDto.setType(StatisticsTypeEnum.DAY.getCode());
                statsCommodityStockRecordDto.setUserId(commodityStockRecordVo.getUserId());
                statsCommodityStockRecordDto.setMerchantStoreId(commodityStockRecordVo.getMerchantStoreId());
                statsCommodityStockRecordDto.setCommodityId(commodityStockRecordVo.getCommodityId());
                statsCommodityStockRecordDto.setFirstType(commodityStockRecordVo.getFirstType());
                statsCommodityStockRecordDto.setSecondaryType(commodityStockRecordVo.getSecondaryType());
                statsCommodityStockRecordDto.setTotalQuantity(commodityStockRecordVo.getQuantity());
                statsCommodityStockRecordDto.setTotalCostPrice(commodityStockRecordVo.getTotalCostPrice());
                statsCommodityStockRecordDto.setTotalRetailPrice(commodityStockRecordVo.getTotalRetailPrice());
                statsCommodityStockRecordDto.setDeviceId(commodityStockRecordVo.getDeviceId());
                Integer result = statsCommodityStockRecordDao.insertStatsCommodityStockRecord(statsCommodityStockRecordDto);
                if (result == 0) {
                    log.info("每日统计昨天的商品库存记录，插入失败，失败对象：{}", JSON.toJSONString(statsCommodityStockRecordDto));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            StatsPackageStoreDto statsPackageStoreDto = new StatsPackageStoreDto();
            statsPackageStoreDto.setStartTime(startTime);
            statsPackageStoreDto.setEndTime(endTime);
            errorLogCommonService.writerInfoException("每日统计昨天的商品库存记录失败", statsPackageStoreDto, e);
        }
    }

    /**
     * 每月统计上月的商品库存记录
     */
    @Override
    public void statsMonthCommodityStockRecordJob() {
        String startDateStr = DateTimeUtils.getPreviousMonthFirstDay();
        String endDateStr = DateTimeUtils.getPreviousMonthLastDay();
        String dateStr = DateTimeUtils.getPreviousMonth();
        try {
            List<StatsCommodityStockRecordVo> list = statsCommodityStockRecordDao.findStatsCommodityStockRecordByTime(startDateStr, endDateStr);
            for (StatsCommodityStockRecordVo statsCommodityStockRecordVo : list) {
                StatsCommodityStockRecordDto statsCommodityStockRecordDto = new StatsCommodityStockRecordDto();
                statsCommodityStockRecordDto.setDateStr(dateStr);
                statsCommodityStockRecordDto.setType(StatisticsTypeEnum.MONTH.getCode());
                statsCommodityStockRecordDto.setUserId(statsCommodityStockRecordVo.getUserId());
                statsCommodityStockRecordDto.setMerchantStoreId(statsCommodityStockRecordVo.getMerchantStoreId());
                statsCommodityStockRecordDto.setCommodityId(statsCommodityStockRecordVo.getCommodityId());
                statsCommodityStockRecordDto.setFirstType(statsCommodityStockRecordVo.getFirstType());
                statsCommodityStockRecordDto.setSecondaryType(statsCommodityStockRecordVo.getSecondaryType());
                statsCommodityStockRecordDto.setTotalQuantity(statsCommodityStockRecordVo.getTotalQuantity());
                statsCommodityStockRecordDto.setTotalRetailPrice(statsCommodityStockRecordVo.getTotalRetailPrice());
                statsCommodityStockRecordDto.setTotalCostPrice(statsCommodityStockRecordVo.getTotalCostPrice());
                statsCommodityStockRecordDto.setDeviceId(statsCommodityStockRecordVo.getDeviceId());
                Integer result = statsCommodityStockRecordDao.insertStatsCommodityStockRecord(statsCommodityStockRecordDto);
                if (result == 0) {
                    log.info("每月统计上月的商品库存记录，插入失败，失败对象：{}", statsCommodityStockRecordDto.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            StatsPackageStoreDto statsPackageStoreDto = new StatsPackageStoreDto();
            statsPackageStoreDto.setStartTime(startDateStr);
            statsPackageStoreDto.setEndTime(endDateStr);
            errorLogCommonService.writerInfoException("每月统计上月的商品库存记录失败", statsPackageStoreDto, e);
        }
    }
}
